﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HBAClient
{
    public partial class Out : Form
    {
        public Out()
        {
			AviMark.WEBProxySoapClient a = new AviMark.WEBProxySoapClient();
            InitializeComponent();
        }
        // Insert new bill record
        private void SaveClick()
        {
            bool passesValidation = true;
            int intSum = 1;
            // Check validation
			if ((txtSum.Text == "") || int.TryParse(txtSum.Text, out intSum) == false || intSum <= 0)
            {
                lblSumError.Visible = true;
                passesValidation = false;
            }
            else
            {
                lblSumError.Visible = false;
            }

            if (cmbSource.Text == "")
            {
                lblSourceError.Visible = true;
                passesValidation = false;
            }
            else
            {
                lblSourceError.Visible = false;
            }

            // Check if validation successfully
            if (passesValidation) // Successfully
            {
                try
                {
                    Util.SaveRecord(Program.GetUserID(), dtDatePick.Value, 0, int.Parse(txtSum.Text),
                    (long)cmbBank.SelectedValue, cmbSource.Text);
                    lblMessage.Text = "Saved successfully";
                }
                catch
                {
                    lblMessage.Text = "Unable to save";
                }
            }
            else // Unsuccessfully
            {
                lblMessage.Text = "Please check the validation messages";
            }
        }
		private void btnSave_Click(object sender, EventArgs e)
		{
            // Insert the bill record
            SaveClick();
            // Recorder the balances in the bills and get the last balance
            int nFinalBalance = Util.RunOrderBills(Program.GetUserID(), Convert.ToInt64(cmbBank.SelectedValue));
            // Update balances table with the last balance
            Util.SaveNewBalance(Program.GetUserID(),Convert.ToInt64(cmbBank.SelectedValue), nFinalBalance);
		}

        private void Out_Load(object sender, EventArgs e)
        {
            // Set the Sources and Banks of current user
            Util.SetSources(cmbSource);
            Util.SetBanks(cmbBank);
        }

        private void txtSum_TextChanged(object sender, EventArgs e)
        {
            // Prapare for new save
            lblMessage.Text = Util.INOUT_DEFAULT_MSG;
        }

        private void dtDatePick_ValueChanged(object sender, EventArgs e)
        {
            // Prapare for new save
            lblMessage.Text = Util.INOUT_DEFAULT_MSG;
        }

        private void cmbBank_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Prapare for new save
            lblMessage.Text = Util.INOUT_DEFAULT_MSG;
        }

        private void cmbSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Prapare for new save
            lblMessage.Text = Util.INOUT_DEFAULT_MSG;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            // Close In form
            this.Close();
        }

        private void MenuSave_Click(object sender, EventArgs e)
        {
            // Insert the bill record
            SaveClick();
            // Recorder the balances in the bills and get the last balance
            int nFinalBalance = Util.RunOrderBills(Program.GetUserID(), Convert.ToInt64(cmbBank.SelectedValue));
            // Update balances table with the last balance
            Util.SaveNewBalance(Program.GetUserID(), Convert.ToInt64(cmbBank.SelectedValue), nFinalBalance);
        }
    }
}
